package com.link.base.base.mpconfig.controller;

import com.link.base.base.mpconfig.model.SendTempMessage;
import com.link.base.base.mpconfig.service.SendTempMessageService;
import com.link.base.base.sms.service.PushSmsService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.RedisUtil;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 闫有霞
 * @date: 2018-12-14
 * @comments: 发送模板消息包括小程序模板消息和微信模板消息
 */
@Controller
@RequestMapping("/link/sendTempMessage")
public class SendTempMessageController extends BasicController<SendTempMessage> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private SendTempMessageService sendTempMessageService;

    @Resource
    private PushSmsService pushSmsService;

    @Override
    public BasicService<SendTempMessage> getBasicService() throws Exception {
        return sendTempMessageService;
    }

    /**
     * @createdBy 闫有霞
     * @date 2018/12/3
     * @methods: sendTemplateMessage
     * @param: mp
     * @return:
     * @description: 发送模板消息
     **/
    @RequestMapping(value = "sendTemplateMessage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object>  sendTemplateMessage(@RequestBody SendTempMessage entity, HttpSession session,
                                                    HttpServletRequest request, HttpServletResponse response) {
        Map<String ,Object>  result = new HashMap<>(16);
        try {
            result = sendTempMessageService.sendTemplateMessage(entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     *@createdBy 闫有霞
     *@date 2018/12/29
     *@methods: saveMessageFormId
     *@param: SendTempMessage entity
     *@return: void
     *@description:  保存formId到redis用于发送小程序模板消息
     **/
    @RequestMapping(value = "saveMessageFormId", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String,Object>  saveMessageFormId(@RequestBody SendTempMessage entity, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String ,Object>  result = new HashMap<>(16);
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            String openId = UserUtil.getUser().getOpenId();
            /**虚拟的fromId非真机产生*/
            String virtualFormId = "the formId is a mock one";
            if (entity.getMessageFormIdList() != null && entity.getMessageFormIdList().size() > 0 && StringUtils.isNotBlank(openId)) {
                List<String> messageFormIdList = entity.getMessageFormIdList();
                for (String item : messageFormIdList ) {
                    /**非真机产生的formId不保存*/
                    if (StringUtils.equals(virtualFormId,item)) {
                        continue;
                    } else {
                        /**设置formId的有效时间为7天*/
                        jedis.setex("MESSAGE_FORM_ID:" + openId + ":" + keyGenerateService.keyGenerate(),604800,item);

                    }
                }

            } else {
                throw  new BasicControllerException("该用户没有openId！");
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        } finally {
            RedisUtil.returnResource(jedis);
        }
        return result;
    }
}
